<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>预约管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form :inline="true" :model="searchParam" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchParam.patientName" placeholder="输入患者姓名"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="searchParam.medicalRecordNo" placeholder="输入病历号"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="searchParam.mobile" placeholder="输入患者手机号"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="roleCode" label="患者姓名" width="120">
      </el-table-column>
      <el-table-column sortable prop="roleCode" label="患者昵称" width="120">
      </el-table-column>
      <el-table-column sortable prop="roleName" label="病历号" width="120">
      </el-table-column>
      <el-table-column sortable prop="mobile" label="手机号" width="120">
      </el-table-column>
      <el-table-column sortable prop="sex" label="性别" width="120">
      </el-table-column>
      <el-table-column sortable prop="updateBy" label="修改人" width="120">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteItem(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-bind:child-msg="pageParam" @callFather="refreshFatherPage"></Pagination>
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="120px" :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="患者姓名" prop="patientName">
          <el-input size="small" v-model="editForm.patientName" auto-complete="off" placeholder="请输入患者姓名"></el-input>
        </el-form-item>
        <el-form-item label="病历号" prop="roleName">
          <el-input size="small" v-model="editForm.medicalRecordNo" auto-complete="off" placeholder="请输入病历号"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
import {getRequest, postRequest} from '../../utils/api'
export default {
  data () {
    return {
      loading: false,
      editFormVisible: false,
      menuAccessShow: false,
      title: '添加',
      editForm: {
        id: '',
        patientName: '',
        medicalRecordNo: '',
        mobile: ''
      },
      rules: {
        patientName: [
          {required: true, message: '请输入患者姓名', trigger: 'blur'}
        ],
        medicalRecordNo: [
          {required: true, message: '请输入病历号', trigger: 'blur'}
        ]
      },
      searchParam: {
        pageNum: 1,
        pageSize: 10,
        medicalRecordNo: '',
        patientName: ''
      },
      listData: [],
      pageParam: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  components: {
    Pagination
  },
  created () {
    this.getData(this.searchParam)
  },
  methods: {
    getData (parameter) {
      this.loading = true
      getRequest('/patient/page', parameter).then(resp => {
        this.loading = false
        if (resp && resp.code === 0 && resp.data) {
          this.listData = resp.data.records
          this.pageParam.currentPage = this.searchParam.pageNum
          this.pageParam.pageSize = this.searchParam.pageSize
          this.pageParam.total = resp.data.total
        }
        // eslint-disable-next-line handle-callback-err
      }).catch(err => {
        this.loading = false
        this.$message.error('获取患者列表失败，请稍后再试！')
      })
    },
    refreshFatherPage (param) {
      this.searchParam.page = param.currentPage
      this.searchParam.limit = param.pageSize
      this.getData(this.searchParam)
    },
    search () {
      this.getData(this.searchParam)
    },
    handleEdit: function (index, row) {
      this.editFormVisible = true
      if (row !== undefined && row !== 'undefined') {
        this.title = '修改'
        this.editForm.id = row.id
        this.editForm.patientName = row.patientName
        this.editForm.medicalRecordNo = row.medicalRecordNo
      } else {
        this.title = '添加'
        this.editForm.id = ''
        this.editForm.patientName = ''
        this.editForm.medicalRecordNo = ''
      }
    },
    submitForm (editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          postRequest('/patient/save', this.editForm).then(resp => {
            this.editFormVisible = false
            this.loading = false
            if (resp && resp.code === 0 && resp.data) {
              this.getData(this.searchParam)
            } else {
              this.$message({
                type: 'info',
                message: resp.msg
              })
            }
            // eslint-disable-next-line handle-callback-err
          }).catch(err => {
            this.loading = false
            this.$message.error('保存患者信息失败，请稍后再试！')
          })
        } else {
          return false
        }
      })
    },
    deleteItem (index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let deleteParam = {'id': row.id}
        postRequest('/patient/delete', deleteParam).then(resp => {
          if (resp && resp.code === 0 && resp.data) {
            this.$message({
              type: 'success',
              message: '患者已删除！'
            })
            this.getData(this.searchParam)
          } else {
            this.$message({
              type: 'info',
              message: resp.msg
            })
          }
          // eslint-disable-next-line handle-callback-err
        }).catch(err => {
          this.loading = false
          this.$message.error('删除患者失败，请稍后再试！')
        })
      })
    },
    closeDialog (dialog) {
      if (dialog === 'edit') {
        this.editFormVisible = false
      } else if (dialog === 'perm') {
        this.menuAccessShow = false
      }
    }
  }
}
</script>
<style scoped>
  .user-search {
    margin-top: 20px;
    text-align: left;
  }
</style>
